home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 3: The Continuation / 17-Bit_The_Continuation_Disc.iso / amigan / amigan 8 / hack.save.c < prev    next >
C/C++ Source or Header  |  1994-01-27  |  5KB  |  218 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
  2.  
  3. #include "hack.h"
  4. extern char genocided[60];    /* defined in Decl.c */
  5. extern char fut_geno[60];    /* idem */
  6. #include <signal.h>
  7.  
  8. extern char SAVEF[], nul[];
  9. extern char pl_character[PL_CSIZ];
  10. extern long lseek();
  11. extern struct obj *restobjchn();
  12. extern struct monst *restmonchn();
  13.  
  14. extern char *index();        /* M.E.T.  11/20/85 */
  15.  
  16. dosave(){
  17.     if(dosave0(0)) {
  18.         settty("Be seeing you ...\n");
  19.         hackexit(0);
  20.     }
  21. #ifdef lint
  22.     return(0);
  23. #endif lint
  24. }
  25.  
  26. #ifndef NOSAVEONHANGUP
  27. hangup(){
  28.     (void) dosave0(1);
  29.     hackexit(1);
  30. }
  31. #endif NOSAVEONHANGUP
  32.  
  33. /* returns 1 if save successful */
  34. dosave0(hu) int hu; {
  35.     register int fd, ofd;
  36.     int tmp;      /* not register ! */
  37.     (void) signal(SIGHUP, SIG_IGN);
  38.     (void) signal(SIGINT, SIG_IGN);
  39.     if((fd = creat(SAVEF, FMASK)) < 0)
  40.         {
  41.         if(!hu) pline("Cannot open save file. (Continue or Quit)");
  42.             return(0);
  43.         }
  44.     makeicon(SAVEF, pl_character[0]);
  45.     savelev(fd);
  46.     saveobjchn(fd, invent);
  47.     saveobjchn(fd, fcobj);
  48.     savemonchn(fd, fallen_down);
  49.     bwrite(fd, (char *) &flags, sizeof(struct flag));
  50.     bwrite(fd, (char *) &dlevel, sizeof dlevel);
  51.     bwrite(fd, (char *) &maxdlevel, sizeof maxdlevel);
  52.     bwrite(fd, (char *) &moves, sizeof moves);
  53.     bwrite(fd, (char *) &u, sizeof(struct you));
  54.     bwrite(fd, (char *) pl_character, sizeof pl_character);
  55.     bwrite(fd, (char *) genocided, sizeof genocided);
  56.     bwrite(fd, (char *) fut_geno, sizeof fut_geno);
  57.     savenames(fd);
  58.     for(tmp = 1; tmp <= maxdlevel; tmp++)
  59.         {
  60.         glo(tmp);
  61.         if((ofd = open(lock, 0)) < 0)
  62.             continue;
  63.         (void) getlev(ofd);
  64.         (void) close(ofd);
  65.         bwrite(fd, (char *) &tmp, sizeof tmp);   /* level number */
  66.         savelev(fd);            /* actual level */
  67.         (void) unlink(lock);
  68.         }
  69.     (void) close(fd);
  70.     *index(lock, '.') = 0;
  71.     (void) unlink(lock);
  72.     return(1);
  73. }
  74.  
  75. dorecover(fd)
  76. register int fd;
  77. {
  78.     register int nfd;
  79.     int tmp;      /* not a register ! */
  80.     struct obj *otmp;
  81.      (void) getlev(fd);
  82.     invent = restobjchn(fd);
  83.     for(otmp = invent; otmp; otmp = otmp->nobj)
  84.         if(otmp->owornmask)
  85.             setworn(otmp, otmp->owornmask);
  86.     fcobj = restobjchn(fd);
  87.     fallen_down = restmonchn(fd);
  88.     mread(fd, (char *) &flags, sizeof(struct flag));
  89.     mread(fd, (char *) &dlevel, sizeof dlevel);
  90.     mread(fd, (char *) &maxdlevel, sizeof maxdlevel);
  91.     mread(fd, (char *) &moves, sizeof moves);
  92.     mread(fd, (char *) &u, sizeof(struct you));
  93.     mread(fd, (char *) pl_character, sizeof pl_character);
  94.     mread(fd, (char *) genocided, sizeof genocided);
  95.     mread(fd, (char *) fut_geno, sizeof fut_geno);
  96.     restnames(fd);
  97.     while(1)
  98.         {
  99.         if(read(fd, (char *) &tmp, sizeof tmp) != sizeof tmp)
  100.             break;
  101.         if(getlev(fd))
  102.             break;      /* this is actually an error */
  103.         glo(tmp);
  104.         if((nfd = creat(lock, FMASK)) < 0)
  105.             panic("Cannot open temp file %s!\n", lock);
  106.         savelev(nfd);
  107.         (void) close(nfd);
  108.         }
  109.     (void) lseek(fd, 0L, 0);
  110.     (void) getlev(fd);
  111.     (void) close(fd);
  112.     (void) unlink(SAVEF);
  113.     (void) delicon(SAVEF);
  114.     if(Punished) {
  115.         for(otmp = fobj; otmp; otmp = otmp->nobj)
  116.             if(otmp->olet == CHAIN_SYM) goto chainfnd;
  117.         panic("Cannot find the iron chain?");
  118.     chainfnd:
  119.         uchain = otmp;
  120.         if(!uball)
  121.             {
  122.             for(otmp = fobj; otmp; otmp = otmp->nobj)
  123.                 if(otmp->olet == BALL_SYM && otmp->spe)
  124.                     goto ballfnd;
  125.             panic("Cannot find the iron ball?");
  126.             ballfnd:
  127.             uball = otmp;
  128.             }
  129.     }
  130. #ifndef QUEST
  131.     setsee();  /* only to recompute seelx etc. - these weren't saved */
  132. #endif QUEST
  133.     docrt();
  134. }
  135.  
  136. struct obj *
  137. restobjchn(fd)
  138. register int fd;
  139. {
  140.     register struct obj *otmp, *otmp2;
  141.     register struct obj *first = 0;
  142.     int xl;
  143. #ifdef lint
  144.     /* suppress "used before set" warning from lint */
  145.     otmp2 = 0;
  146. #endif lint
  147.     while(1)
  148.         {
  149.         mread(fd, (char *) &xl, sizeof(xl));
  150.         if(xl == -1) break;
  151.         otmp = newobj(xl);
  152.         if(!first) first = otmp;
  153.         else otmp2->nobj = otmp;
  154.         mread(fd, (char *) otmp, (unsigned) xl + sizeof(struct obj));
  155.         if(!otmp->o_id)   /* from MKLEV */
  156.             otmp->o_id = flags.ident++;
  157.         otmp2 = otmp;
  158.         }
  159.     if(first && otmp2->nobj)
  160.         {
  161.         pline("Restobjchn: error reading objchn.");
  162.         impossible();
  163.         otmp2->nobj = 0;
  164.         }
  165.     return(first);
  166. }
  167.  
  168. struct monst *
  169. restmonchn(fd)
  170. register int fd;
  171. {
  172.     register struct monst *mtmp, *mtmp2;
  173.     register struct monst *first = 0;
  174.     int xl;
  175.     
  176. #ifdef FUNNYRELOC
  177.     struct permonst *monbegin;
  178.  
  179.     mread(fd, (char *)&monbegin, sizeof(monbegin));
  180. #endif
  181.  
  182. #ifdef lint
  183.     /* suppress "used before set" warning from lint */
  184.     mtmp2 = 0;
  185. #endif lint
  186.     while(1)
  187.         {
  188.         mread(fd, (char *) &xl, sizeof(xl));
  189.         if(xl == -1) break;
  190.         mtmp = newmonst(xl);
  191.         if(!first) first = mtmp;
  192.         else mtmp2->nmon = mtmp;
  193.         mread(fd, (char *) mtmp, (unsigned) xl + sizeof(struct monst));
  194. #ifdef DEBUGMON
  195.         myprintf("Read Monster #%d", mtmp->data);
  196. #endif
  197.         mtmp->data = &mons[ (int) mtmp->data ];
  198.         if(!mtmp->m_id) {         /* from MKLEV */
  199.             mtmp->m_id = flags.ident++;
  200. #ifndef NOWORM
  201.             if(mtmp->data->mlet == 'w' && getwn(mtmp)){
  202.             initworm(mtmp);
  203.             mtmp->msleep = 0;
  204.             }
  205. #endif NOWORM
  206.         }
  207.         if(mtmp->minvent)
  208.             mtmp->minvent = restobjchn(fd);
  209.         mtmp2 = mtmp;
  210.         }
  211.     if(first && mtmp2->nmon){
  212.         pline("Restmonchn: error reading monchn.");
  213.         impossible();
  214.         mtmp2->nmon = 0;
  215.     }
  216.     return(first);
  217. }
  218.